//导入依赖包

const express= require('express');
const common = require('../../../libs/common');

const superagent = require("superagent");
const cheerio = require("cheerio");


let router = express.Router();
global.detailData='';
module.exports = router;


router.post('/',(req,res)=>{
    superagent.get("http://www.jimmyjazz.com/search/FW15HP")
    .end((error, response) => {
        if(response){
            
            //获取页面文档数据
            var content = response.text;
            //cheerio也就是nodejs下的jQuery  将整个文档包装成一个集合，定义一个变量$接收
            let $ = cheerio.load(content);
            //定义一个空数组，用来接收数据
            let result=[[],[],[]];
            let i=0;
            //分析文档结构  先获取每个li 再遍历里面的内容(此时每个li里面就存放着我们想要获取的数据)
            $("#container>.content_area_wrapper2>.content_area_wrapper>.content_container>.content>.category>ul>li").each((index,value)=>{
                if($(value).find('.product_grid_price >.product_price').text()!=''){
                    if(i<3){
                        result[0].push({
                            img_src:$(value).find('.product_grid_image>a>img').attr('src'),
                            title:$(value).find('.product_grid_info>.product_grid_brand>a').text(),
                            price:common.returnFloat($(value).find('.product_grid_price >.product_price').text().replace(/[\$]/g,""))
                        })
                    }else if(i>=3&&i<9){
                        result[1].push({
                            img_src:$(value).find('.product_grid_image>a>img').attr('src'),
                            title:$(value).find('.product_grid_info>.product_grid_brand>a').text(),
                            price:common.returnFloat($(value).find('.product_grid_price >.product_price').text().replace(/[\$]/g,""))
                        })
                    }else if(i>=9&&i<12){
                        result[2].push({
                            img_src:$(value).find('.product_grid_image>a>img').attr('src'),
                            title:$(value).find('.product_grid_info>.product_grid_brand>a').text(),
                            price:common.returnFloat($(value).find('.product_grid_price >.product_price').text().replace(/[\$]/g,""))
                        })
                    }
                    i++;
                }
                
            })
            detailData=result;
            res.send(result)
        }
    })
})
